﻿using System;
using Team4Mock.Entity;
using System.Data;

namespace Team4Mock.UserControl.Team
{
    public partial class AddTeam : System.Web.UI.UserControl
    {
        TeamEntity te = new TeamEntity();
        OrganisationEntity oe = new OrganisationEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Convert.ToString(Session["role"])))
                {
                    if (Session["role"].ToString().Equals("Admin"))
                    {
                        btnDel.Visible = true;
                    }
                    else
                    {
                        btnDel.Visible = false;
                    }
                }
                else
                {
                    btnDel.Visible = false;
                }

                dlCountry.DataSource = oe.GetCountry();
                dlCountry.DataBind();
                if (!string.IsNullOrEmpty(Request.QueryString["t"]))
                {
                    btnDel.Visible = true;
                    int id = int.Parse(Request.QueryString["t"]);
                    DataTable dt = new DataTable();
                    te.TeamID = id;
                    dt = te.GetdataByIDTeam(te);
                    if (dt.Rows.Count > 0)
                    {
                        txtTeamName.Text = dt.Rows[0][1].ToString();
                        txtDesShort.Text = dt.Rows[0][2].ToString();
                        idContact.Value = dt.Rows[0][3].ToString();
                        txtAdd1.Text = dt.Rows[0][4].ToString();
                        txtAdd2.Text = dt.Rows[0][5].ToString();
                        txtAdd3.Text = dt.Rows[0][6].ToString();
                        PostCode.Value = dt.Rows[0][7].ToString();
                        Town.Value = dt.Rows[0][8].ToString();
                        County.Value = dt.Rows[0][9].ToString();
                        dlCountry.SelectedValue = dt.Rows[0][10].ToString();
                        hdidBus.Value = dt.Rows[0][11].ToString();
                        hdsic.Value = dt.Rows[0][12].ToString();
                        txtDesFull.Text = dt.Rows[0][13].ToString();
                        txtPhone.Text = dt.Rows[0][14].ToString();
                        txtFax.Text = dt.Rows[0][15].ToString();
                        txtWeb.Text = dt.Rows[0][16].ToString();
                        txtCharNum.Text = dt.Rows[0][17].ToString();
                        txtComNum.Text = dt.Rows[0][18].ToString();
                        if (dt.Rows[0][20].ToString() == "1")
                        {
                            ckIsActive.Checked = true;
                        }
                        else
                        {
                            ckIsActive.Checked = false;
                        }

                    }
                }
                else
                {
                    btnDel.Visible = false;
                }

            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Department.aspx?D=" + Request.QueryString["D"] + "&s=" + Request.QueryString["s"] + "&ID=" + Request.QueryString["ID"]);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtTeamName.Text == "")
            {
                tb.Text = "Input Department Name";
            }
            else
            {
                if (rdOrg.Checked == true || rdParent.Checked == true)
                {
                    Copy();
                }
                else
                {
                    te.TeamName = txtTeamName.Text;
                    te.TeamShortDescription = txtDesShort.Text;
                    te.ContactID = int.Parse(idContact.Value);
                    te.AddressLine1 = txtAdd1.Text;
                    te.AddressLine2 = txtAdd2.Text;
                    te.AddressLine3 = txtAdd3.Text;
                    te.Postcode = PostCode.Value;
                    te.Town = Town.Value;
                    te.County = County.Value;
                    te.Country = dlCountry.SelectedValue;
                    te.TypeOfBusiness = hdidBus.Value;
                    te.SICCode = hdsic.Value;
                    te.TeamFullDescription = txtDesFull.Text;
                    te.PhoneNumber = txtPhone.Text;
                    te.Fax = txtFax.Text;
                    te.WebAddress = txtWeb.Text;
                    te.CharityNumber = txtCharNum.Text;
                    te.CompanyNumber = txtComNum.Text;
                    te.IsActive = ckIsActive.Checked ? 1 : 0;
                }
                //Create
                if (string.IsNullOrEmpty(Request.QueryString["t"]))
                {
                    te.DepartmentID = int.Parse(Request.QueryString["D"]);
                    te.Insert(te);
                    tb.Text = "Insert Success";
                }
                //Amend
                else
                {
                    te.TeamID = int.Parse(Request.QueryString["t"]);
                    te.Update(te);
                    tb.Text = "Update Success";
                }
            }
        }
        void Copy()
        {
            string name = txtTeamName.Text, shortdes = txtDesShort.Text, bus = hdidBus.Value, sic = hdsic.Value, contactid = "", full = txtDesFull.Text, orgid = Request.QueryString["ID"], depart = Request.QueryString["D"];
            string add1 = "", add2 = "", add3 = "", postcode = "", town = "", county = "", country = "", phone = "", fax = "", web = "", charity = "", company = "";

            int idorg = int.Parse(orgid);
            int iddepart = int.Parse(depart);

            DataTable dt = new DataTable();
            if (rdOrg.Checked == true)
            {
                te.OrgID = idorg;
                dt = te.CopyOrg(te);
            }
            if (rdParent.Checked == true)
            {
                te.DepartmentID = iddepart;
                dt = te.CopyDepart(te);
            }
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                add1 = dt.Rows[0][0].ToString();
                add2 = dt.Rows[0][1].ToString();
                add3 = dt.Rows[0][2].ToString();
                postcode = dt.Rows[0][3].ToString();
                town = dt.Rows[0][4].ToString();
                county = dt.Rows[0][5].ToString();
                country = dt.Rows[0][6].ToString();
                phone = dt.Rows[0][7].ToString();
                fax = dt.Rows[0][8].ToString();
                web = dt.Rows[0][9].ToString();
                charity = dt.Rows[0][10].ToString();
                company = dt.Rows[0][11].ToString();
                contactid = dt.Rows[0][12].ToString();
            }
            te.TeamName = name;
            te.TeamShortDescription = shortdes;
            te.ContactID = int.Parse(contactid);
            te.AddressLine1 = add1;
            te.AddressLine2 = add2;
            te.AddressLine3 = add3;
            te.Postcode = postcode;
            te.Town = town;
            te.County = county;
            te.Country = country;
            te.TypeOfBusiness = bus;
            te.SICCode = sic;
            te.TeamFullDescription = full;
            te.PhoneNumber = phone;
            te.Fax = fax;
            te.WebAddress = web;
            te.CharityNumber = charity;
            te.CompanyNumber = company;
            te.IsActive = ckIsActive.Checked ? 1 : 0;

        }

        protected void btnDel_Click(object sender, EventArgs e)
        {

            try
            {
                int id = int.Parse(Request.QueryString["t"]);
                te.TeamID = id;
                te.Del(te);
                Response.Redirect("/FormView/Department.aspx?D=" + Request.QueryString["D"] + "&s=" + Request.QueryString["s"] + "&ID=" + Request.QueryString["ID"]);
            }
            catch
            {
                tb.Text = "Error while being processed";
            }
        }
    }
}